package cn.kxlove.zhbj;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

/**
 * @author kxlove 16/1/28 17:47
 */
public class NewsDetailActivity extends Activity implements View.OnClickListener{

    private ImageButton btnBack;
    private ImageButton btnTextSize;
    private ImageButton btnShare;
    private LinearLayout llControl;

    private WebView mWebView;
    private String mUrl;
    private ProgressBar pbLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_news_detail);

        btnBack = (ImageButton) findViewById(R.id.btn_back);
        llControl = (LinearLayout) findViewById(R.id.ll_control);
        btnTextSize = (ImageButton) findViewById(R.id.btn_text_size);
        btnShare = (ImageButton) findViewById(R.id.btn_share);
        pbLoading = (ProgressBar) findViewById(R.id.pb_loading);

        btnBack.setOnClickListener(this);
        btnTextSize.setOnClickListener(this);
        btnShare.setOnClickListener(this);

        mWebView = (WebView) findViewById(R.id.webview);

        findViewById(R.id.btn_menu).setVisibility(View.GONE);// 隐藏menu按钮

        btnBack.setVisibility(View.VISIBLE);
        llControl.setVisibility(View.VISIBLE);

        WebSettings settings = mWebView.getSettings();
        settings.setBuiltInZoomControls(true);// 显示放大缩小按钮(仅限于非移动端适配的页面)
        settings.setUseWideViewPort(true);// 支持双击缩放(仅限于非移动端适配的页面)
        settings.setJavaScriptEnabled(true);// 支持js

        mUrl = getIntent().getStringExtra("url");
        mWebView.loadUrl(mUrl);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // 开始加载网页
                pbLoading.setVisibility(View.VISIBLE);// 显示进度条
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // <a href="tel:110">联系我们</a>
                // 解析url, 如果发现以tel:开头,读取出电话号码,跳到打电话页面,从而实现网页和本地代码的交互
                // 网页跳转时回调此方法
                // 点击超级链接之后,跳转页面时
                System.out.println("url:" + url);
                return super.shouldOverrideUrlLoading(view, url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                System.out.println("onPageFinished");
                // 网页加载结束
                pbLoading.setVisibility(View.GONE);// 隐藏进度条
                super.onPageFinished(view, url);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                // 获取网页标题
                System.out.println("title:" + title);
                super.onReceivedTitle(view, title);
            }

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // 获取网页进度
                System.out.println("progress:" + newProgress);
                super.onProgressChanged(view, newProgress);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_text_size:
                // 修改网页字体大小
                showChooseDialog();
                break;

            case R.id.btn_share:
                // 分享 ShareSDK
                showShare();
                break;

            default:
                break;
        }
    }
    private int mClickItem;// 被点击选择的字体位置

    private int mSelectedItem = 2;// 被最终选择的字体大小的位置, 默认是正常字体大小
    private void showChooseDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("字体设置");

        String[] items = new String[] { "超大号字体", "大号字体", "正常字体", "小号字体",
                "超小号字体" };
        builder.setSingleChoiceItems(items, mSelectedItem,
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mClickItem = which;
                    }
                });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                WebSettings settings = mWebView.getSettings();
                switch (mClickItem) {
                    case 0:
                        settings.setTextSize(WebSettings.TextSize.LARGEST);
                        // settings.setTextZoom(10);
                        break;
                    case 1:
                        settings.setTextSize(WebSettings.TextSize.LARGER);
                        break;
                    case 2:
                        settings.setTextSize(WebSettings.TextSize.NORMAL);
                        break;
                    case 3:
                        settings.setTextSize(WebSettings.TextSize.SMALLER);
                        break;
                    case 4:
                        settings.setTextSize(WebSettings.TextSize.SMALLEST);
                        break;

                    default:
                        break;
                }

                mSelectedItem = mClickItem;
            }
        });

        builder.setNegativeButton("取消", null);
        builder.show();
    }

    private void showShare() {
    }
}
